package com.yunhe.authority.controller.authority;

import com.yunhe.authority.domain.authority.Menu;
import com.yunhe.authority.domain.authority.MenuUnBindStationVo;
import com.yunhe.authority.service.authority.MenuService;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单Controller
 * @author liuronglei
 */
@Api(value = "menu",description = "菜单维护", tags = {"Authority_Menu"})
@RestController
@RequestMapping("/menus")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 新增菜单
     * @param menu 菜单对象
     * @param parentId 父菜单ID
     * ok
     */
    @ApiOperation("新增菜单")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody Menu menu,
                                             @RequestParam(value = "parentId", required = false) Long parentId)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        Menu newMenu = menuService.createMenu(menu, parentId);
        return new ResponseEntity<>(new ResultObject<>(newMenu), HttpStatus.OK);
    }


    /**
     * 根据菜单ID删除菜单
     * @param ids 菜单ID
     * ok
     */
    @ApiOperation("根据菜单ID删除菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "菜单ID", required = true, dataType = "String", paramType = "path")
    })
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> deleteByIds(@PathVariable("ids") String ids) {
        menuService.deleteMenuByIds(ids);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    /**
     * 更新菜单（更新所有字段）
     * @param id 菜单ID
     * @param menu 菜单对象
     * ok
     */
    @ApiOperation("更新菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = true, dataType = "Long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> put(@PathVariable("id") Long id,
                                            @RequestBody Menu menu,
                                            @RequestParam(value = "parentId", required = false) Long parentId)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        menu.setId(id);
        Menu newMenu = menuService.updateMenu(menu, parentId);
        return new ResponseEntity<>(new ResultObject<>(newMenu), HttpStatus.OK);
    }


    /**
     * 根据菜单ID获得菜单
     * @param id 菜单ID
     * ok
     */
    @ApiOperation("根据菜单ID获得菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = true, dataType = "Long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        Menu menu = menuService.findByMenuId(id);
        return new ResponseEntity<>(new ResultObject<>(menu), HttpStatus.OK);
    }

    /**
     * 根据角色ID获得菜单
     * @param roleIds 多个角色ID
     * ok
     */
    @ApiOperation("根据多个角色ID获得菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleIds", value = "多个角色ID", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/searchMenusByRoleId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findMenusByRoleId(@RequestParam(value = "roleIds", required = true) String roleIds) {
        List<Menu> menu = menuService.findMenusByRoleId(roleIds);
        return new ResponseEntity<>(new ResultObject<>(menu), HttpStatus.OK);
    }

    /**
     * 查询所有菜单列表
     * ok
     */
    @ApiOperation("查询所有菜单列表")
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search() {
        Iterable<Menu> results = menuService.findMenus();
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 根据项目id或者单位id查询菜单列表
     * 查询所有菜单列表
     * ok
     */
    @ApiOperation("根据项目id或者单位id查询菜单列表")
    @RequestMapping(value = "/searchByCondition", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "项目ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "firmId", value = "单位ID", required = false, dataType = "Long", paramType = "query")
    })
    public ResponseEntity<ResultObject> searchByStationId(@RequestParam(value = "stationId", required = false) Long stationId,
                                                          @RequestParam(value = "firmId", required = false) Long firmId)
                                                            throws ArgumentErrorException {
        Iterable<Menu> results = menuService.findMenusByStationIdOrFirmId(stationId, firmId);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 根据项目id或者单位id再加上角色id查询菜单列表
     * 查询所有菜单列表
     * ok
     */
    @ApiOperation("根据项目id或者单位id再加上角色id查询菜单列表")
    @RequestMapping(value = "/findMenusByStationIdOrFirmIdAndRoleId", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "项目ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "firmId", value = "单位ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Long", paramType = "query")
    })
    public ResponseEntity<ResultObject> findMenusByStationIdOrFirmIdAndRoleId(@RequestParam(value = "stationId", required = false) Long stationId,
                                                                              @RequestParam(value = "firmId", required = false) Long firmId,
                                                                              @RequestParam(value = "roleId", required = true) Long roleId)
                                                            throws ArgumentErrorException {
        Iterable<Menu> results = menuService.findMenusByStationIdOrFirmIdAndRoleId(stationId, firmId, roleId);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 项目绑定/解绑菜单
     * ok
     */
    @ApiOperation("项目绑定/解绑菜单")
    @RequestMapping(value = "/menuBindAndUnbindStation", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> menuBindStation(@RequestBody MenuUnBindStationVo menuUnBindStationVo)
            throws ObjectNotFoundException, ArgumentErrorException {
        menuService.menusBindStationOrFirm(menuUnBindStationVo);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    /**
     * 根据系统查询菜单列表
     * ok
     */
    @ApiOperation("根据系统查询菜单列表")
    @RequestMapping(value = "/findMenusBySystem/{systemMenuCategory}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findMenusBySystem(@PathVariable("systemMenuCategory") String systemMenuCategory) {
        List<Menu> results = menuService.findMenusBySystem(systemMenuCategory);
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 置顶菜单
     * ok
     */
    @ApiOperation("置顶菜单")
    @RequestMapping(value = "/top/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> top(@PathVariable("id") Long id) {
        menuService.topMenu(id);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }
}